package org.fireflow2.designer.eclipse.palette;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.gef.palette.ConnectionCreationToolEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PaletteStack;
import org.eclipse.gef.palette.PaletteToolbar;
import org.eclipse.gef.palette.SelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.internal.Workbench;
import org.fireflow.pdl.fpdl20.diagram.Fpdl20DiagramElement;
import org.fireflow2.designer.eclipse.Activator;


public class PaletteFactory {
    public static PaletteRoot createPalette() {
    	PaletteToolbar toolBar = new PaletteToolbar("");
    	ToolEntry tool = new SelectionToolEntry();
    	toolBar.add(tool);
    	
        List<PaletteContainer> categories = new ArrayList<PaletteContainer>();
        categories.add(toolBar);
        categories.add(createWorkflowEntries());
        categories.add(createServiceEntries());
    	
        PaletteRoot paletteRoot = new PaletteRoot();
        paletteRoot.addAll(categories);
        return paletteRoot;
    }
    
    public static PaletteRoot createSimulatorPalette(){
        PaletteRoot paletteRoot = new PaletteRoot();
        paletteRoot.addAll(createSimulatorCategories(paletteRoot));
        return paletteRoot;
    }

    //********************************************************//
    private static PaletteStack createRouterStack(){
    	PaletteStack controlGroup = new PaletteStack("Routers(Gateways)","All routers(gateways)",null);

    	List<ToolEntry> entries = new ArrayList<ToolEntry>();
        ImageDescriptor smallIcon = null;
        

        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/empty_gateway.png");
        ToolEntry tool = new CombinedTemplateCreationEntry(" Empty Router", "Create a empty router", new DiagramElementFactory(
        		Fpdl20DiagramElement.ROUTER,DiagramElementSubType.EMPTY), smallIcon, null);
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/exclusive_gateway_2.png");
        tool = new CombinedTemplateCreationEntry(" Exclusive Router", "Create a exclusive router", new DiagramElementFactory(
        		Fpdl20DiagramElement.ROUTER,DiagramElementSubType.ROUTER_EXCLUSIVE), smallIcon, null);
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/inclusive_gateway.png");
        tool = new CombinedTemplateCreationEntry(" Inclusive Router", "Create a inclusive router", new DiagramElementFactory(
        		Fpdl20DiagramElement.ROUTER,DiagramElementSubType.ROUTER_INCLUSIVE), smallIcon, null);
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/parallel_gateway.png");
        tool = new CombinedTemplateCreationEntry(" Parallel Router", "Create a parallel router", new DiagramElementFactory(
        		Fpdl20DiagramElement.ROUTER,DiagramElementSubType.ROUTER_PARALLEL), smallIcon, null);
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/complex_gateway.png");
        tool = new CombinedTemplateCreationEntry(" Complex Router", "Create a complex router", new DiagramElementFactory(
        		Fpdl20DiagramElement.ROUTER,DiagramElementSubType.ROUTER_COMPLEX), smallIcon, null);
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
        controlGroup.addAll(entries);
    	return controlGroup;
    }
    
    private static PaletteStack createStartNodeStack(){
    	PaletteStack controlGroup = new PaletteStack("开始节点","开始节点",null);

    	List<ToolEntry> entries = new ArrayList<ToolEntry>();
        ImageDescriptor smallIcon = null;
        

        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/empty_start_event.png");
        ToolEntry tool = new CombinedTemplateCreationEntry(" Start Node", "Create a empty start-node", new DiagramElementFactory(
        		Fpdl20DiagramElement.START_NODE,DiagramElementSubType.EMPTY), smallIcon, null);
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);   
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/timer_start_event.png");
        tool = new CombinedTemplateCreationEntry(" Timer Start", "Create a timer start-node", new DiagramElementFactory(
        		Fpdl20DiagramElement.START_NODE,DiagramElementSubType.START_TIMER_START), smallIcon, null);
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);   
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/message_start_event.png");
        tool = new CombinedTemplateCreationEntry(" WebService Start", "Create a webservice start-node", new DiagramElementFactory(
        		Fpdl20DiagramElement.START_NODE,DiagramElementSubType.START_WS_START), smallIcon, null);
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);   
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/compensation_start_event.png");
        tool = new CombinedTemplateCreationEntry(" Catch Compensation", "Create a catch-compensation start-node", new DiagramElementFactory(
        		Fpdl20DiagramElement.START_NODE,DiagramElementSubType.START_CATCH_COMPENSATION_START), smallIcon, null);
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/error_start_event.png");
        tool = new CombinedTemplateCreationEntry(" Catch Fault", "Create a catch-fault start-node", new DiagramElementFactory(
        		Fpdl20DiagramElement.START_NODE,DiagramElementSubType.START_CATCH_FAULT_START), smallIcon, null);
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
        controlGroup.addAll(entries);
    	return controlGroup;
    }
    
    private static PaletteStack createEndNodeStack(){
    	PaletteStack controlGroup = new PaletteStack("All End Nodes","所有结束节点",null);
   	
    	List<ToolEntry> entries = new ArrayList<ToolEntry>();
        ImageDescriptor smallIcon = null;        
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/end_none_event.png");
        ToolEntry tool = new CombinedTemplateCreationEntry(" End Node", "Create a empty end-node", new DiagramElementFactory(
        		Fpdl20DiagramElement.END_NODE,DiagramElementSubType.EMPTY), smallIcon, null);
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);      
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/end_compensation_event.png");
        tool = new CombinedTemplateCreationEntry(" Throw Compensation", "Create a throw-compensation end-node ",  new DiagramElementFactory(
        		Fpdl20DiagramElement.END_NODE,DiagramElementSubType.END_THROW_COMPENSATION_END), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/end_error_event.png");
        tool = new CombinedTemplateCreationEntry(" Throw Fault", "Create a throw-fault end-node ",  new DiagramElementFactory(
        		Fpdl20DiagramElement.END_NODE,DiagramElementSubType.END_THROW_FAULT_END), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/end_terminate_event.png");
        tool = new CombinedTemplateCreationEntry(" Throw Termination", "Create a throw-termination end-node ",  new DiagramElementFactory(
        		Fpdl20DiagramElement.END_NODE,DiagramElementSubType.END_TERMINATION), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
        controlGroup.addAll(entries);
    	return controlGroup;
    }
    private static PaletteStack createConnectorStack(){
    	PaletteStack controlGroup = new PaletteStack("All Connectors","所有连接",null);
    	
    	List<ToolEntry> entries = new ArrayList<ToolEntry>();
    	ImageDescriptor smallIcon = null;
        
    	smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/sequence_flow.png");
    	ToolEntry tool = new ConnectionCreationToolEntry(" Transition", "Create a transition(Sequence flow)", new DiagramElementFactory(
        		Fpdl20DiagramElement.TRANSITION,DiagramElementSubType.EMPTY), smallIcon, null);   
        entries.add(tool);      
               
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/message_flow.png");
        tool = new ConnectionCreationToolEntry(" Message Flow", "Create a message flow", new DiagramElementFactory(
        		Fpdl20DiagramElement.MESSAGE_FLOW,DiagramElementSubType.EMPTY), smallIcon, null);   
        entries.add(tool);   
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/association.png");
        tool = new ConnectionCreationToolEntry(" Association", "Create a association", new DiagramElementFactory(
        		Fpdl20DiagramElement.ASSOCIATION,DiagramElementSubType.EMPTY), smallIcon, null);   
        entries.add(tool);   
        
        controlGroup.addAll(entries);
        return controlGroup;
    }
    
    private static PaletteContainer createServiceEntries(){
    	PaletteDrawer controlGroup = new PaletteDrawer("服务组件");
    	
    	List<ToolEntry> entries = new ArrayList<ToolEntry>();
    	ImageDescriptor smallIcon = null;
        
    	//人工活动
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/userconfig.png");
        ToolEntry tool = new CombinedTemplateCreationEntry(" 人工活动", "创建 一个人工活动",  new DiagramElementFactory(
        		Fpdl20DiagramElement.ACTIVITY,DiagramElementSubType.SERVICE_HUMAN), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
    	//Java调用
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/service_java.gif");
        tool = new CombinedTemplateCreationEntry(" Java类", "创建 一个调用Java类的活动",  new DiagramElementFactory(
        		Fpdl20DiagramElement.ACTIVITY,DiagramElementSubType.SERVICE_JAVA), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
        //SpringBean 调用
        /*
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/SpringBean.jpg");
        tool = new CombinedTemplateCreationEntry(" SpringBean", "创建 一个调用SpringBean的活动",  new DiagramElementFactory(
        		Fpdl20DiagramElement.ACTIVITY,DiagramElementSubType.SERVICE_JAVA), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        */
        
        //WebService调用
        /*
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/WebService.png");
        tool = new CombinedTemplateCreationEntry(" WebService", "创建 一个调用WebService的活动",  new DiagramElementFactory(
        		Fpdl20DiagramElement.ACTIVITY,DiagramElementSubType.SERVICE_WS), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        */
        
        //Email发送
        /*
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/email.png");
        tool = new CombinedTemplateCreationEntry(" Email发送", "创建 一个发送Email的活动",  new DiagramElementFactory(
        		Fpdl20DiagramElement.ACTIVITY,DiagramElementSubType.SERVICE_EMAIL), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);
        */  
       
        //子流程
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/subflow.png");
        tool = new CombinedTemplateCreationEntry(" 子流程", "创建 一个调用子流程的活动",  new DiagramElementFactory(
        		Fpdl20DiagramElement.ACTIVITY,DiagramElementSubType.SERVICE_EMAIL), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
    	
        controlGroup.addAll(entries);
    	return controlGroup;
   }
    
    private static PaletteContainer createWorkflowEntries() {
    	List entries = new ArrayList();
    	
    	PaletteDrawer controlGroup = new PaletteDrawer("流程元素");
        ImageDescriptor smallIcon = null;
        

        entries.add(createStartNodeStack());        
        entries.add(createEndNodeStack());
        entries.add(createRouterStack());
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/activity.png");
        ToolEntry tool = new CombinedTemplateCreationEntry(" Activity", "Create a empty activity ",  new DiagramElementFactory(
        		Fpdl20DiagramElement.ACTIVITY,DiagramElementSubType.EMPTY), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
                
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/group.png");
        tool = new CombinedTemplateCreationEntry(" Group", "Create a group",new DiagramElementFactory(
        		Fpdl20DiagramElement.GROUP,DiagramElementSubType.EMPTY), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);             
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/pool.png");
        tool = new CombinedTemplateCreationEntry(" Pool", "Create a pool",new DiagramElementFactory(
        		Fpdl20DiagramElement.POOL,DiagramElementSubType.EMPTY), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);    
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/lane.png");
        tool = new CombinedTemplateCreationEntry(" Lane", "Create a lane",new DiagramElementFactory(
        		Fpdl20DiagramElement.LANE,DiagramElementSubType.EMPTY), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);   
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/obj16/annotation.png");
        tool = new CombinedTemplateCreationEntry(" Comment", "Create a comment",  new DiagramElementFactory(
        		Fpdl20DiagramElement.COMMENT,DiagramElementSubType.EMPTY), smallIcon, null);   
        tool.setToolClass(MyCreationTool.class);
        entries.add(tool);  
        
        entries.add(createConnectorStack());
        
        ///////////////////////////////////////
   
        controlGroup.addAll(entries);
        return controlGroup;
    }

    private static List createSimulatorCategories(PaletteRoot root) {
        List categories = new ArrayList();

        categories.add(createSimulatorControlGroup(root));
//        categories.add(createComponentsDrawer());

        return categories;
    }
    

    private static PaletteContainer createSimulatorControlGroup(PaletteRoot root) {
        PaletteGroup controlGroup = new PaletteGroup("Default Group");
        ImageDescriptor smallIcon = null;
        ImageDescriptor largeIcon = null;
        
        List entries = new ArrayList();
        ToolEntry tool = new SelectionToolEntry();
        entries.add(tool);
        root.setDefaultEntry(tool);  
        
        smallIcon = ImageDescriptor.createFromFile(Activator.class, "resources/activity16.gif");
        PaletteEntry createNewWorkflowProcess = new PaletteEntry("Create","",smallIcon,null);
        entries.add(createNewWorkflowProcess);
        createNewWorkflowProcess.addPropertyChangeListener(new PropertyChangeListener(){

			public void propertyChange(PropertyChangeEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println("createNewWorkflowProcess editReturnBindingExpressionButton clicked.......");
			}
        	
        });
        
        controlGroup.addAll(entries);        
        return controlGroup;
    }
}
